package uplift.project;

import java.util.ArrayList;
import java.util.Calendar;

import uplift.project.model.VolunteerEvent;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextThemeWrapper;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

public class FindEventActivity extends Activity {
   
	private Spinner categorySpinner;
	private EditText locationEditText;
	private EditText dateEditText;
	private EditText startsEditText;
	private EditText endsEditText;
	
    private int mYear;
    private int mMonth;
    private int mDay;
    
    private int sHour;
    private int sMinute;
    private String sMeridiem;
    
    private int eHour;
    private int eMinute;
    private String eMeridiem;
	
	static final int DATE_DIALOG_ID = 1;
	static final int START_TIME_DIALOG_ID = 2;
	static final int END_TIME_DIALOG_ID = 3;
	
	private static ArrayList<VolunteerEvent> list;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_event);
        
        categorySpinner = (Spinner) this.findViewById(R.id.spinner_category);
        locationEditText = (EditText) this.findViewById(R.id.editText_location);
        dateEditText = (EditText) this.findViewById(R.id.editText_date);
        startsEditText = (EditText) this.findViewById(R.id.editText_starts);
        endsEditText = (EditText) this.findViewById(R.id.editText_ends);
       
        categorySpinner = (Spinner) findViewById(R.id.spinner_category);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.category_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        categorySpinner.setAdapter(adapter);
        
        dateEditText.setClickable(true);
        dateEditText.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);
            }
        });
        
        startsEditText.setClickable(true);
        startsEditText.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(START_TIME_DIALOG_ID);
            }
        });
        
        endsEditText.setClickable(true);
        endsEditText.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(END_TIME_DIALOG_ID);
            }
        });
        
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
    }
    
    
 	@Override
 	protected Dialog onCreateDialog(int id) {
 	        if (id == DATE_DIALOG_ID) {
                 return new DatePickerDialog(this, AlertDialog.THEME_HOLO_DARK,
                         mDateSetListener, mYear, mMonth, mDay);
 	        } else if (id == START_TIME_DIALOG_ID) {
 		        return new TimePickerDialog(this, AlertDialog.THEME_HOLO_DARK,
 		        		sTimeSetListener, 12, 00, false);
 	        } else if (id == END_TIME_DIALOG_ID) {
 		        return new TimePickerDialog(this, AlertDialog.THEME_HOLO_DARK,
 		        		eTimeSetListener, 12, 00, false);
 	        }
 	        return null;
 	}
	
	protected void onPrepareDialog(int id, Dialog dialog) {
	        if (id == DATE_DIALOG_ID) {
                ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
	        } else if (id == START_TIME_DIALOG_ID) {
	        	((TimePickerDialog) dialog).updateTime(sHour, sMinute);
	        } else if (id == END_TIME_DIALOG_ID) {
	        	((TimePickerDialog) dialog).updateTime(eHour, eMinute);
	        }
	}
	

	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		public void onDateSet(DatePicker view, int year, int monthOfYear,
		                int dayOfMonth) {
		        mYear = year;
		        mMonth = monthOfYear;
		        mDay = dayOfMonth;
		        updateDate();
		}
	};
	
	private TimePickerDialog.OnTimeSetListener sTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {			
			if (hourOfDay < 12) {
				sMeridiem = "am";
			} else {
				sMeridiem = "pm";
			}
			
			sHour = hourOfDay;
			sMinute = minute;
			updateStartTime();
		}
	};
	
	private TimePickerDialog.OnTimeSetListener eTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			if (hourOfDay < 12) {
				eMeridiem = "am";
			} else {
				eMeridiem = "pm";
			}
			
			eHour = hourOfDay;
			eMinute = minute;
			updateEndTime();
		}
	};
	
    private void updateDate() {
        dateEditText.setText(
                new StringBuilder()
                // Month is 0 based so add 1
                .append(getMonthName(mMonth)).append(" ")
                .append(mDay).append(", ")
                .append(mYear));
    }
    
    private void updateStartTime() {
        StringBuilder sb = new StringBuilder();
        
        int tempHour = sHour %12;
        
        if (tempHour == 0) {
        	tempHour = 12;
        }
        
        sb.append(tempHour).append(":");
        
        if (sMinute < 10) {
        	sb.append("0");
        }

        sb.append(sMinute).append(sMeridiem);
    	
        startsEditText.setText(sb.toString());
    }
    
    private void updateEndTime() {
        StringBuilder sb = new StringBuilder();
        
        int tempHour = eHour %12;
        
        if (tempHour == 0) {
        	tempHour = 12;
        }
        
        sb.append(tempHour).append(":");
        
        if (eMinute < 10) {
        	sb.append("0");
        }

        sb.append(eMinute).append(eMeridiem);
    	
        endsEditText.setText(sb.toString());
    }

	
    // goes to the preview events screen
    public void searchResults(View v) {
    	Intent i = new Intent(v.getContext(), SearchResultsActivity.class);
    	i.putExtra("category", categorySpinner.getSelectedItem().toString());
    	i.putExtra("date", dateEditText.getText().toString());
    	i.putExtra("location", locationEditText.getText().toString());
    	i.putExtra("starts", startsEditText.getText().toString());
    	i.putExtra("ends", endsEditText.getText().toString());
	
    	startActivity(i);
    }
    
    // Clears search fields
    public void clearFields(View v) {
    	categorySpinner.setSelection(0);
    	dateEditText.setText("");
    	locationEditText.setText("");
    	startsEditText.setText("");
    	endsEditText.setText("");
    	
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        
		sHour = 0;
		sMinute = 0;
		
		eHour = 0;
		eMinute = 0;
    }
    
    protected void alertbox(String title, String mymessage) {
    new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Holo_Dialog))
       .setMessage(mymessage)
       .setTitle(title)
       .setNeutralButton(android.R.string.ok,
          new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int whichButton){}
          })
       .show();
    }
    
    private String getMonthName(int m) {
    	if (m == 0) {
    		return "January";
    	} else	if (m == 1) {
    		return "February";
    	} else	if (m == 2) {
    		return "March";
    	} else	if (m == 3) {
    		return "April";
    	} else	if (m == 4) {
    		return "May";
    	} else	if (m == 5) {
    		return "June";
    	} else	if (m == 6) {
    		return "July";
    	} else	if (m == 7) {
    		return "August";
    	} else	if (m == 8) {
    		return "September";
    	} else	if (m == 9) {
    		return "October";
    	} else	if (m == 10) {
    		return "November";
    	} else	if (m == 11) {
    		return "December";
    	} else {
    		// Should never actually reach here.
    		return "Imaginary Month";
    	}
    }
    
}
